package com.zyq.validation.validator;

import com.zyq.validation.annotation.CannotBlank;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
public class CannotBlankValidator implements ConstraintValidator<CannotBlank, String> {
    public boolean isValid(String value, ConstraintValidatorContext context) {
        //null时不进行校验
         if(value != null  &&  value.contains(" ")){//如果value包含空格就不能通过验证。
             //获取默认提示信息
             String defaultConstraintMessgeTemplate=context.getDefaultConstraintMessageTemplate();
             System.out.println("default message :"+defaultConstraintMessgeTemplate);
             //禁用默认提示信息
             context.disableDefaultConstraintViolation();
             //设置提示语
             context.buildConstraintViolationWithTemplate("can not contains blank-不能包含空格").addConstraintViolation();
             return false;
         }
         return true;
    }
}
